package initialization

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"onerk4/zk1/global"
	"onerk4/zk1/model"
)

func MysqlInit() {
	var err error
	nac := Configs.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", nac.User, nac.Password, nac.Host, nac.Port, nac.Data)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic(err)
	}
	err = global.DB.AutoMigrate(&model.User{}, &model.Order{}, &model.Hotel{}, &model.HotelRoomType{})
	fmt.Println("mysql init success")

}
